<?php
class Administrator_SeogroupController extends Zend_Controller_Action
{
	public function init() {

	}

	public function indexAction(){
		$objSeogroup = new HT_Model_administrator_models_seogroup();
		$do = @$this->_request->getParam('do');
		$id = (int)$this->_request->getParam('id');
		if($do == 'list'){
			$this->getListKeyword();
		}elseif($do == 'delete'){
			$this->deleteSeogroup($id);
		}else{
			$keyword = $this->_request->getParam('keyword');
			$this->view->keyword 	= $keyword;
		}
		$this->view->inlineScript()->appendFile(WEB_PATH.'/application/modules/administrator/views/scripts/seogroup/index.js');
	}
	
	function deleteSeogroup($id){
		$objSeogroup = new HT_Model_administrator_models_seogroup();
		$data = array('is_deleted'=>1);
		echo $objSeogroup->update($data,"group_id=".(int)$id);die();
	}
	
	public function updateAction(){
		$objSeogroup 	= new HT_Model_administrator_models_seogroup();
		$objUtil 		= new HT_Model_administrator_models_utility();
		$do 		 	= @$this->_request->getParam('do');
		$id 			= (int)$this->_request->getParam('id');
		$status 		= (int)$this->_request->getParam('status');
		@$auth 			= Zend_Auth::getInstance();
		$user 			= @$auth->getStorage()->read();
		$wishid			= $user->wishid;
		
		$pageUrl			= null;
		if($do == 'submit'){
			$data = array();
			$data['subject'] 				= $this->_request->getParam('subject');
			$data['keyword'] 				= $this->_request->getParam('keyword');
			$data['secondary_keyword'] 		= $this->_request->getParam('secondary_keyword');
			$data['tag_title'] 				= $this->_request->getParam('tag_title');
			$data['tag_description'] 		= $this->_request->getParam('tag_description');
			$data['active'] 				= $this->_request->getParam('active');
			$data['total_mark'] 			= $this->_request->getParam('total_mark');
			if($id >0){
				$data['date_updated'] 			= date('Y-m-d H:i:s');
				$data['admin_update_id'] 		= $wishid;
				$return = $objSeogroup->updateData($data,$id);
			}else{
				$data['date_created'] 			= date('Y-m-d H:i:s');
				$data['date_updated'] 			= date('Y-m-d H:i:s');
				$data['admin_create_id'] 		= $wishid;
				$return = $objSeogroup->addData($data);
			}
				
			if($return > 0){
				$this->_redirect(WEB_PATH.'/administrator/seogroup/update?status=1&id='.$return);
			}else{
				$this->_redirect(WEB_PATH."/administrator/seogroup/update?status=$return&id=$id");
			}
				
		}elseif($id >0){
			$objConvert				= new HT_Model_administrator_models_convert();
			$seogroup 				= $objSeogroup->getSeogroup($id,array('getRelatedNews'=>true));
			$group_id				= (int)$seogroup['group_id'];
			$subject				= trim($seogroup['subject']);
			$keyword				= trim($seogroup['keyword']);
			if($subject){
				$pageUrl				= WEB_PATH.'/chu-de-nen-doc/'.$objConvert->utf8_to_url($subject).'-'.$group_id.'.html';
			}else{
				$pageUrl				= WEB_PATH.'/chu-de-nen-doc/'.$objConvert->utf8_to_url($keyword).'-'.$group_id.'.html';
			}
				
				
			$this->view->seogroup	= $seogroup;
			$this->view->pageUrl	= $pageUrl;
		}
	
		$this->view->id 		= $id;
		$this->view->status 	= $status;
		$this->view->inlineScript()->appendFile(WEB_PATH.'/application/modules/administrator/views/scripts/seogroup/update.js');
	}
	
	public function suggestAction(){
		$objSeogroup = new HT_Model_administrator_models_seogroup();
		$objSeogroup->suggestGroup(1000,3);
	}
	
	function getListKeyword(){
		$objUtil 		= new HT_Model_administrator_models_utility();
		$objConvert		= new HT_Model_administrator_models_convert();
		$objSeogroup 	= new HT_Model_administrator_models_seogroup();
		$keyword 		= trim($this->_request->getParam('keyword'));
	
		$page 			= (int)$this->_request->getParam('page');
		$size 			= PAGING_SIZE;
		if (!is_numeric($page) || $page <= 0) {
			$page = 1;
		}
		$start = $page * $size - $size;
	
		$filter = array('is_deleted'=>0);
		if($keyword) $filter['keyword'] = $keyword;
			
		$totalRecord = $objSeogroup->getListSeogroup_nb($filter);
		$listSeogroup = $objSeogroup->getListSeogroup($start,$size,$filter);
		$paging = trim($objUtil->paging($page, $size, $totalRecord));
	
		$ajaxData = null;
		if($paging){
			$ajaxData .= '<div class="paging_box">';
			$ajaxData .= $paging;
			$ajaxData .= '<div class="cb"></div></div>';
		}
	
		$ajaxData .= '<table cellspacing="0" class="tablesorter tablesorterBorder">';
		$ajaxData .= '<thead>';
		$ajaxData .= '<tr>';
		$ajaxData .= '<th width="15">STT</th>';
		$ajaxData .= '<th width="200">Chủ đề</th>';
		$ajaxData .= '<th width="150">Từ khóa</th>';
		$ajaxData .= '<th width="20">M</th>';
		$ajaxData .= '<th width="150">Từ khóa phụ</th>';
		$ajaxData .= '<th width="300">Title</th>';
		$ajaxData .= '<th width="300">Description</th>';
		$ajaxData .= '<th width="60">Active</th>';
		$ajaxData .= '<th style="white-space: nowrap;padding-right: 5px;" align="center">Điều khiển</th>';
		$ajaxData .= '</tr>';
		$ajaxData .= '</thead>';
	
		$i=0;
		foreach($listSeogroup as $seogroup){
			$group_id				= (int)$seogroup['group_id'];
			$subject				= trim($seogroup['subject']);
			$keyword				= trim($seogroup['keyword']);
			if($subject){
				$pageUrl				= WEB_PATH.'/chu-de-nen-doc/'.$objConvert->utf8_to_url($subject).'-'.$group_id.'.html';
			}else{
				$pageUrl				= WEB_PATH.'/chu-de-nen-doc/'.$objConvert->utf8_to_url($keyword).'-'.$group_id.'.html';
			}
			
			$lengthTitle 			= strlen($subject);
			$active					= $seogroup['active'];	
			if($active == 1){
				$active = "OK";
			}else{
				$active = '';
			}
			$i++;
			$trClass = null;
			if($i%2 == 1) $trClass = ' class="altrow"';
			$ajaxData .= '<tr id="'.$seogroup['group_id'].'" '.$trClass.'>';
			$ajaxData .= '<input type="hidden" name="group_ids[]" value="'.$seogroup['group_id'].'" />';
			$ajaxData .= '<td align="center">'.$i.'</td>';
			$ajaxData .= '<td><a href="'.$pageUrl.'" target="_blank">'.$seogroup['subject'].'</td>';
			$ajaxData .= '<td>'.$seogroup['keyword'].'</td>';
			$ajaxData .= '<td style="color:#FF0000; font-weight:bold;">'.$seogroup['total_mark'].'</td>';
			$ajaxData .= '<td>'.$seogroup['secondary_keyword'].'</td>';
			$ajaxData .= '<td>'.$seogroup['tag_title'].'</td>';
			$ajaxData .= '<td>'.$seogroup['tag_description'].'</td>';
			$ajaxData .= '<td align="center">'.$active.'</td>';
			$ajaxData .= '<td style="white-space: nowrap" align="center">';
			$ajaxData .= '<a href="#" onclick="deleteSeogroup('.$seogroup['group_id'].')">Xóa</a> | <a href="'.WEB_PATH.'/administrator/seogroup/update/?id='.$seogroup['group_id'].'">Cập nhật</a>';
			$ajaxData .= '</td>';
			$ajaxData .= '</tr>';
		}
		$ajaxData .= '</tbody>';
		$ajaxData .= '</table>';
		$ajaxData .= '</div>';
		if($paging){
			$ajaxData .= '<div class="paging_box">';
			$ajaxData .= $paging;
			$ajaxData .= '<div class="cb"></div></div>';
		}
		
	
		$searchResult = "Có <b>".number_format($totalRecord, 0, '.', ',')."</b> kết quả phù hợp với điều kiện tìm kiếm";
		echo $ajaxData.':::'.$searchResult; die();
	}
}
